<?php
    //Kiểm tra quyền admin
    require('../lib/admin_auth.php');
    //Gọi cấu hình site
    require('../config.php');
    /**
     * ********************
     * Gọi phần Đầu trang*
     * ********************/
     //Chuẩn bị thông tin trước khi gọi
     $page_title = "Trang Chủ";
     $script = array(
     "Functions" => "function.js");
     /////////////////////////////////
    require('themes/header.php');
    /**
     * ********************
     * Gọi phần nội dung trang*
     * ********************/
     //Kết nối database
     require('../lib/db_connect.php');
     //Xử lý dữ liệu
     $query = "SELECT SUM(order_price) FROM order_item";
     $result = mysql_query($query, $con);
     $data = mysql_fetch_assoc($result);
     $doanhthu = $data['SUM(order_price)'];
?>
<table class="stats">
    <tr>
        <td class="result title">Doanh thu</td>
        <td class="result"><?=number_format($doanhthu)?> VNĐ</td>  
    </tr>
    <?php
        $query = "SELECT * FROM order_item as o, item as i WHERE o.item_name=i.item_name";
        $result = mysql_query($query, $con);
        $giagoc = 0;
        while ($data = mysql_fetch_assoc($result)){
            $giagoc += $data['item_price']*$data['order_quantity'];
        }
        $lai = $doanhthu - $giagoc;
    ?>
    <tr>
        <td class="result title">Lãi thật</td>
        <td class="result"><?=number_format($lai)?> VNĐ</td>
    </tr>
    <?php
        $query = "SELECT * FROM item";
        $result = mysql_query($query, $con);
        $vondautu = 0;
        while ($data = mysql_fetch_assoc($result)) {
            $vondautu += $data['item_price']*$data['item_quantity'];
        }
        
    ?>
    <tr>
        <td class="result title">Vốn đầu tư</td>
        <td class="result"><?=number_format($vondautu)?> VNĐ</td>
    </tr>
    <?php
        $query = "SELECT item_name, SUM(order_quantity) FROM order_item GROUP BY item_name ORDER BY order_quantity DESC";
        $result = mysql_query($query, $con);
        $data = mysql_fetch_assoc($result);
        $highest = $data['item_name'];
    ?>
    <tr>
        <td class="result title">Mua nhiều nhất</td>
        <td class="result"><?=$highest?></td>
    </tr>
    <?php
        $query = "SELECT item_name, SUM(order_quantity) FROM order_item GROUP BY item_name ORDER BY order_quantity ASC";
        $result = mysql_query($query, $con);
        $data = mysql_fetch_assoc($result);
        $lowest = $data['item_name'];
    ?>
    <tr>
        <td class="result title">Mua ít nhất</td>
        <td class="result"><?=$lowest?></td>
    </tr>
</table>



<?php    
    /**
     * ********************
     * Gọi phần cuối trang*
     * ********************/
    require('themes/footer.php');
    
?>

        
        